﻿using ServerBase.Client;
using ServerBase.Config;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilLib;
using UtilPublic.ApiDispatcher;
using UtilPublic.Serialization;
using static ToolClient.DataClient;

namespace ToolClient
{
    /// <summary>
    /// PageLogin.xaml 的交互逻辑
    /// </summary>
    public partial class PageLogin : Page
    {
        public PageLogin()
        {
            InitializeComponent();

            ComboBoxServer1.ComboBox.ItemsSource = (from kvp in Conf.ConfToolServerGameExt
                                           where ((EServerType)kvp.Value.ServerType == EServerType.游戏 && kvp.Value.ServerOpen)
                                           select (kvp.Key));
            //ComboBoxServer1.SelectedIndex = 0;
            ComboBoxServer1.ComboBox.SelectedIndex = 1;

            TextBoxGameName.TextboxText = RandomName();

            MainWindow.绑定控制器(this);
        }

        public void SendInfoGetAll()
        {
            var Req = new C2G_Info_GetAll();
            App.ClientNetGame.Send(Req);
        }

        #region 游戏服登录
        List<string> ListNameString = new List<string>()
        {
            "啊","滴","呜","嘻","呵","叮","璐",
        };
        public string RandomName()
        {
            string result = "";
            for (int i = 0; i < 4; i++)
            {
                result += ListNameString.RandomSingle();
            }
            return result;
        }
        private void Button_Click_ServerGameConn(object sender, RoutedEventArgs e)
        {
            if (!Conf.ConfToolServerGameExt.TryGetValue(ComboBoxServer1.ComboBoxText, out var conf))
            {
                return;
            }
            App.ClientNetGame.Setup(conf.ServerIp, conf.ServerPort);
            if (App.ClientNetGame.Start())
            {
                //Info("连接成功");
            }
            else
            {
                App.DebugError("连接失败");
                return;
            }
        }
        private void Button_Click_ServerGameLogin(object sender, RoutedEventArgs e)
        {
            var Req = new C2L_Login_UserLogin()
            {
                Account = TextBoxGameAccount.TextboxText,
                Password = TextBoxGamePasswd.TextboxText,
            };
            App.ClientNetGame.Send(Req);
        }
        private void Button_Click_ServerGameLoginPuid(object sender, RoutedEventArgs e)
        {
            var Req = new C2G_Login_PuidLogin()
            {
                Puid = TextBoxGamePuid.TextboxText.ToLong(),
            };
            Req.Pin = Util.SimpleVerifyCode(Req.Puid);
            App.ClientNetGame.Send(Req);
        }
        [ApiDpPro((short)EProtocolId.L2C_LOGIN_USERLOGIN, "登陆用户账号")]
        public short OnLoginUserLogin()
        {
            var Rsp = LunarClientDispatcher.OnMsg.To<L2C_Login_UserLogin>();
            if (Rsp.Success) { App.DebugDebug("登陆用户账号 成功"); } else { App.DebugError(Rsp.Result); return 1; }

            Uuid = Rsp.Uuid;
            Code = Rsp.Code;

            App.WdMain.ColumnGridNav.Width = new GridLength(100);
            App.WdMain.GridNav.Visibility = Visibility.Visible;
            App.WdMain.PageOn("PagePlayer");

            var Req = new C2G_Login_PlayerLogin()
            {
                Uuid = Uuid,
                Code = Code,
            };
            App.ClientNetGame.Send(Req);
            return 0;
        }
        [ApiDpPro((short)EProtocolId.G2C_LOGIN_PLAYERLOGIN, "登陆玩家")]
        public short OnLoginPlayerLogin()
        {
            var Rsp = LunarClientDispatcher.OnMsg.To<G2C_Login_PlayerLogin>();
            if (Rsp.Shuttle != "create")
            {
                App.DebugDebug("玩家登录成功");
            }
            else
            {
                var Req = new C2G_Login_PlayerCreate()
                {
                    Name = RandomName(),
                    Gender = "女性",
                    Family = "南海普陀山",
                };
                App.ClientNetGame.Send(Req);
                return 1;
            }

            SendInfoGetAll();
            return 0;
        }
        [ApiDpPro((short)EProtocolId.G2C_LOGIN_PLAYERCREATE, "玩家创建")]
        public short OnLoginPlayerCreate()
        {
            var Rsp = LunarClientDispatcher.OnMsg.To<G2C_Login_PlayerCreate>();
            if (Rsp.Success) { App.DebugDebug("玩家创建成功"); } else { App.DebugError(Rsp.Result); return 1; }

            SendInfoGetAll();
            return 0;
        }
        [ApiDpPro((short)EProtocolId.G2C_LOGIN_PUIDLOGIN, "管理员使用PUID直接登录")]
        public short OnLoginPuidLogin()
        {
            var Rsp = LunarClientDispatcher.OnMsg.To<G2C_Login_PuidLogin>();
            if (Rsp.Success) { App.DebugDebug("玩家登录成功"); } else { App.DebugError(Rsp.Result); return 1; }

            App.WdMain.ColumnGridNav.Width = new GridLength(100);
            App.WdMain.GridNav.Visibility = Visibility.Visible;
            App.WdMain.PageOn("PagePlayer");

            SendInfoGetAll();
            return 0;
        }

        #endregion
    }
}
